package com.fast.mybatis.service.impl;

import com.fast.mybatis.TemplateUtil;
import com.fast.mybatis.service.FastService;
import com.fast.mybatis.util.*;
import com.lkx.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author likaixuan
 * @Description
 * @Create Date  2018-09-06 22:24
 * @Version 1.0
 */
@Service
public class FastServiceImpl implements FastService {

    @Value("${file.download.path}")
    private String basePath;

    /**
     * @param [mysqlEntity, rootPath, daoPath, servicePath, webPath, mapperPath, entityPath, author]
     * @return void
     * @Function 生成dao, service, controller
     * @author likaixuan
     * @Date 2018-10-22 09:15
     */
    @Override
    public void generateFile(MysqlEntity mysqlEntity, String rootPath, String daoPath, String servicePath, String webPath, String mapperPath, String entityPath, String author) throws Exception {
        String[] tableNames = mysqlEntity.getTableName().split(",");

        //MySqlDBUtil mySqlDBUtil = new MySqlDBUtil(mysqlEntity);
//        List<TableFiledEntity> list  = mySqlDBUtil.getColumnNames(mysqlEntity.getDbName(), tableNames[0], "_");
//
//        TableFiledEntity tableFiledEntity = list.get(0);
//        System.out.println(tableFiledEntity.getDbFiledName()+" - "+tableFiledEntity.getDbFiledType()+" - "+tableFiledEntity.getFiledMark());
//        ExcelUtil.exportExcel("D:\\"+tableNames[0]+".xlsx",list,TableFiledEntity.class);
        for (int i = 0; i < tableNames.length; i++) {
            List<TableFiledEntity> list = new ArrayList<>();
            MySqlDBUtil mySqlDBUtil = new MySqlDBUtil(mysqlEntity);
            list = mySqlDBUtil.getColumnNames(mysqlEntity.getDbName(), tableNames[i], "_");
            try {
                //生成实体类
                TemplateUtil.getBean(list, rootPath + "." + entityPath, StringUtil.toUpperCaseFirstOne(tableNames[i]), author, basePath);
                //生成BaseDao
                TemplateUtil.getBaseDao(rootPath + "." + daoPath, StringUtil.toUpperCaseFirstOne(tableNames[i]), author, basePath);
                //生成Dao
                TemplateUtil.getDao(list, rootPath + "." + daoPath, StringUtil.toUpperCaseFirstOne(tableNames[i]), rootPath + "." + entityPath, author, basePath);
                //生成Mapper.xml
                TemplateUtil.getMapper(list, rootPath + "." + daoPath, rootPath + "." + mapperPath, rootPath + "." + entityPath, tableNames[i], author, basePath);
                //生成Service
                TemplateUtil.getService(list, rootPath + "." + servicePath, rootPath + "." + entityPath, StringUtil.toUpperCaseFirstOne(tableNames[i]), author, basePath);
                //生成ServiceImpl
                TemplateUtil.getServiceImpl(list, rootPath + "." + servicePath, rootPath + "." + entityPath, rootPath + "." + daoPath, StringUtil.toUpperCaseFirstOne(tableNames[i]), StringUtil.processColnames(tableNames[i]), author, basePath);
                //生成Controller
                //TemplateUtil.getBean(list,rootPath+"."+entityPath, StringUtil.toUpperCaseFirstOne(mysqlEntity.getTableName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /*FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("D:/fastMyBatis.zip"));
            ZipUtil.toZip("D:/likaixuan", fos,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null != fos){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/


    }


}
